# Created Time:    2019-04-17 13:44:41
# Modified Time:   2019-04-18 14:46:08
cmake_minimum_required(VERSION 2.7.4 FATAL_ERROR)
project(DEMO)


add_compile_options(-g)
add_compile_options(-std=gnu99)
add_compile_options(-Wall)
add_compile_options(-Wno-char-subscripts)

# 头文件目录
include_directories(common hal/include sal/include)

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)

if(STM32)
    message("cross compile stm32 ...")
    
    set(MCU_FLAGS "-mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mthumb")
    set(CMAKE_C_FLAGS "${MCU_FLAGS} -Wall -O0 -g -std=gnu99")
    set(CMAKE_CXX_FLAGS "${MCU_FLAGS} -Wall -O0 -g -std=gnu99")
    add_compile_options(-fdata-sections)
    add_compile_options(-ffunction-sections)
    add_compile_options(-gdwarf-2)
 
    include_directories(
        stlib/Core/Inc
        stlib/Drivers/STM32F4xx_HAL_Driver/Inc
        stlib/Drivers/STM32F4xx_HAL_Driver/Inc/Legacy
        stlib/Drivers/CMSIS/Device/ST/STM32F4xx/Include
        stlib/Drivers/CMSIS/Include
        stlib/FATFS/Target
        stlib/FATFS/App
        stlib/LWIP/App
        stlib/LWIP/Target
        stlib/Middlewares/Third_Party/LwIP/src/include
        stlib/Middlewares/Third_Party/LwIP/system
        stlib/Middlewares/Third_Party/FreeRTOS/Source/include
        stlib/Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2
        stlib/Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F
        stlib/Middlewares/Third_Party/FatFs/src
        stlib/Middlewares/Third_Party/LwIP/src/include/netif/ppp
        stlib/Middlewares/Third_Party/LwIP/src/include/lwip
        stlib/Middlewares/Third_Party/LwIP/src/include/lwip/apps
        stlib/Middlewares/Third_Party/LwIP/src/include/lwip/priv
        stlib/Middlewares/Third_Party/LwIP/src/include/lwip/prot
        stlib/Middlewares/Third_Party/LwIP/src/include/netif
        stlib/Middlewares/Third_Party/LwIP/src/include/compat/posix
        stlib/Middlewares/Third_Party/LwIP/src/include/compat/posix/arpa
        stlib/Middlewares/Third_Party/LwIP/src/include/compat/posix/net
        stlib/Middlewares/Third_Party/LwIP/src/include/compat/posix/sys
        stlib/Middlewares/Third_Party/LwIP/src/include/compat/stdc
        stlib/Middlewares/Third_Party/LwIP/system/arch
    )

    # 添加子目录
    add_subdirectory(common)
    add_subdirectory(hal)
    add_subdirectory(sal)

    # 指定生成目标 
    link_directories(${CMAKE_CURRENT_SOURCE_DIR}/stlib)
    add_executable(${PROJECT_NAME}.elf main.c)
    target_link_libraries(${PROJECT_NAME}.elf sal hal common st)

    set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
    set(ELF_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf)
    set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
    set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

    #    set_property(SOURCE stlib/Core/Startup/startup_stm32f407igtx.s PROPERTY LANGUAGE C)
    set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/stlib/STM32F407ZGTx_FLASH.ld)
    set(CMAKE_EXE_LINKER_FLAGS
        "-T${LINKER_SCRIPT} --specs=nosys.specs -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map -Wl,--gc-sections --specs=nano.specs -Wl,--start-group -lc -lm -Wl,--end-group"
    )

    # 生成hex和bin文件并输出显示文件大小
    add_custom_command(TARGET "${PROJECT_NAME}.elf" POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O binary ${ELF_FILE} ${BIN_FILE}
        COMMAND ${CMAKE_OBJCOPY} -O ihex  ${ELF_FILE} ${HEX_FILE}
        COMMENT "Building ${PROJECT_NAME}.bin and ${PROJECT_NAME}.hex"
        COMMAND ${CMAKE_COMMAND} -E copy ${HEX_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.hex"
        COMMAND ${CMAKE_COMMAND} -E copy ${BIN_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.bin"

        COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex
        COMMENT "Invoking: Cross ARM GNU Print Size"
    )
else(STM32)
    # 添加子目录
    add_subdirectory(common)
    add_subdirectory(hal)
    add_subdirectory(sal)

    # 指定生成目标
    add_executable(Demo main.c)

    # 添加链接库
    target_link_libraries(Demo sal hal common pthread rt)

endif(STM32)

